package guava;

import com.google.common.base.*;
import com.google.common.collect.Maps;
import org.junit.Test;
import org.xukai.common.ComparableStudent;

import java.io.*;
import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
 * @author xukai
 * @desc
 * @date 2016-11-28-上午 7:31
 */
public class StringUtil {


    /**
     * Joiner
     */
    @Test
    public void testMapJoiner() {
        //Using LinkedHashMap so that the original order is preserved
        String expectedString = "Washington D.C=Redskins#New York City=Giants#Philadelphia=Eagles#Dallas=Cowboys";
        Map<String,String> testMap = Maps.newLinkedHashMap();
        testMap.put("Washington D.C","Redskins");
        testMap.put("New York City","Giants");
        testMap.put("Philadelphia","Eagles");
        testMap.put("Dallas","Cowboys");
        String returnedString = Joiner.on("#").withKeyValueSeparator("=").join(testMap);
        assertEquals(expectedString,returnedString);
    }

    @Test
    public void testStringJoiner() {
        //Using LinkedHashMap so that the original order is preserved
        List<String> list = Arrays.asList("a","b");
//        ArrayList<Object> list = new ArrayList<>();
        Joiner joiner = Joiner.on("|").skipNulls();
        String returnedString = joiner.join(list);
        String expectedString = "a|b";
        assertEquals(expectedString,returnedString);
    }

    @Test
    public void testAppendableJoiner() throws IOException {
        //append到StringBuilder
        StringBuilder stringBuilder = new StringBuilder();
        Joiner joiner = Joiner.on(",").skipNulls();
        joiner.appendTo(stringBuilder, "appendTo", "StringBuilder");
        System.out.println(stringBuilder); //appendTo,StringBuilder

        //append到输出流
        FileWriter writer = new FileWriter("详情json.txt");
        joiner.appendTo(writer, "appendTo", "FileWriter","xukai");
        writer.close();
    }

    /**
     * Splitter
     */
    @Test
    public void testSplitter() {
        String startString = "Washington D.C=Redskins#New York City=Giants#Philadelphia=Eagles#Dallas=Cowboys";
        Map<String,String> expectedMap = Maps.newLinkedHashMap();
        expectedMap.put("Washington D.C","Redskins");
        expectedMap.put("New York City","Giants");
        expectedMap.put("Philadelphia","Eagles");
        expectedMap.put("Dallas","Cowboys");

        Splitter.MapSplitter mapSplitter =Splitter.on("#").trimResults().withKeyValueSeparator("=");
        Map<String,String> returnedMap = mapSplitter.split(startString);
        assertEquals(expectedMap,returnedMap);
    }

    /**
     * Strings
     */
    @Test
    public void testStrings() {
        System.out.println(Strings.isNullOrEmpty(""));//true
        System.out.println(Strings.nullToEmpty(null));//""
        System.out.println(Strings.nullToEmpty("ay"));//"ay"
        System.out.println(Strings.emptyToNull(""));//null
        System.out.println(Strings.emptyToNull("ay"));//"ay"
        System.out.println(Strings.commonPrefix("aaay", "aal"));//"aa"否则返回""
        System.out.println(Strings.commonSuffix("aaay", "aal"));//"aac"否则返回""
        String returnedString = Strings.padEnd("dad", 6, '!');
        String expectedString = "dad!!!";
        assertEquals(expectedString,returnedString);
    }

    @Test
    public void testNullToEmpty() {
        String returnedString = Strings.nullToEmpty(null);
        String expectedString = "";
        assertEquals(expectedString,returnedString);
        System.out.println(Strings.isNullOrEmpty("  "));
    }

    /**
     * CharMatcher
     */
    @Test
    public void testTrimRemoveWhiteSpace(){
        String tabsAndSpaces = "        String     with    spaces  and     tabs";
        String expectedString = "String with spaces and tabs";
        String returnedString = CharMatcher.WHITESPACE.trimAndCollapseFrom(tabsAndSpaces,' ');
        assertEquals(expectedString,returnedString);
    }

    @Test
    public void testRetainFrom(){
        String lettersAndNumbers = "   foo  989y  xbar2  34     ";
        String expectedString = "989234";
        CharMatcher matcher = CharMatcher.JAVA_LETTER.or(CharMatcher.WHITESPACE);
        String returnedString = matcher.removeFrom(lettersAndNumbers);
        assertEquals(expectedString,returnedString);
    }

    @Test
    public void testReplace(){
        String lettersAndNumbers = "adad.afaf.www.ff";
        String s = CharMatcher.is('.').replaceFrom(lettersAndNumbers, '\\');
        String s1 = "adad\\afaf\\www\\ff";
        assertEquals(s1,s);

    }

    /**
     * Preconditions
     */
    public static void getPersonByPrecondition(int age,String neme)throws Exception{
        Preconditions.checkNotNull(neme, "neme为null");
        Preconditions.checkArgument(neme.length()>0, "neme为\'\'");
        Preconditions.checkArgument(age>0, "age 必须大于0");
        System.out.println("a person age:"+age+",neme:"+neme);
    }
    @Test
    public void test2() throws IOException, ClassNotFoundException {
        //序列化对象
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\objectFile.obj"));
        Customer customer = new Customer("王麻子", 24);
        customer.setData(new ComparableStudent("xxx",313,13));
        out.writeObject("你好!");    //写入字面值常量
        out.writeObject(new Date());    //写入匿名Date对象
        out.writeObject(customer);    //写入customer对象
        out.close();


        //反序列化对象
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:\\objectFile.obj"));
        System.out.println("obj1 " + (String) in.readObject());    //读取字面值常量
        System.out.println("obj2 " + (Date) in.readObject());    //读取匿名Date对象
        Customer obj3 = (Customer) in.readObject();    //读取customer对象
        System.out.println("obj3 " + obj3);
        System.out.println(obj3.getData());
        in.close();
    }

}
